//
//  HDHopeModeyView.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/9/8.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}



class HDHopeModeyView: UIView {
    
    var messageLabel = UILabel()
    var moneyTextField = UITextField()
    var moneyNumber = String()
    
    func setupTextFieldView(){
        self.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(0)
            make.height.equalTo(14)
            make.width.equalTo(125)
        }
        messageLabel.text = "此次申请理赔金额："
        messageLabel.font = UIFont.systemFont(ofSize: 13)
        messageLabel.textColor = UIColor(hex:0x666666)
        messageLabel.textAlignment = .left
        
        addSubview(moneyTextField)
        moneyTextField.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(messageLabel.snp.right).offset(5)
            make.right.equalTo(0)
            make.height.equalTo(31)
        }
        moneyTextField.backgroundColor = UIColor.white
        moneyTextField.layer.borderWidth = 0.5
        moneyTextField.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        moneyTextField.layer.cornerRadius = 1
        moneyTextField.keyboardType = .default
        moneyTextField.delegate = self
        moneyTextField.placeholder = " 当前最高理赔限额¥" + moneyNumber + "元"
        moneyTextField.setValue(UIColor(hex:0xa9a9a9), forKeyPath: "_placeholderLabel.textColor")
        moneyTextField.setValue(UIFont.boldSystemFont(ofSize: 14), forKeyPath: "_placeholderLabel.font")
        moneyTextField.setValue(NSNumber(value: 12 as Int32 as Int32), forKey: "paddingLeft")

        moneyTextField.textColor = UIColor(hex:0xa9a9a9)
        moneyTextField.textAlignment = .left
    }
    
    
    func setupMessageView(){
        self.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(0)
            make.height.equalTo(14)
            make.right.equalTo(0)
        }
        messageLabel.text = "此次申请理赔账单金额：" + moneyNumber + "元"
        messageLabel.font = UIFont.systemFont(ofSize: 13)
        messageLabel.textColor = UIColor(hex:0x666666)
        messageLabel.textAlignment = .left
        
        
        
    }


}

extension HDHopeModeyView:UITextFieldDelegate{
    

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        
        var str = String()
        if string.characters.count == 0 {
            str = textField.text!
        }else{
            
            if textField.text == "0"{
                
                if string != "."{
                    str =  string
                    textField.text = ""
                }else{
                    str = textField.text! + string
                }
                

            }else{
                str = textField.text! + string

            }
            
        }
        if str.floatValue > moneyNumber.floatValue{
            
            let strMaxMoney = "您最高可申请的赔付金额为" + moneyNumber + "元，请重新填写"
            
            SVProgressHUD.showError(strMaxMoney)
            return false
        }
//        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        //replacingCharacters(in: range, with: string)
        let expression = "^[0-9]*((\\.|,)[0-9]{0,2})?$"
        let regex = try! NSRegularExpression(pattern: expression, options: NSRegularExpression.Options.allowCommentsAndWhitespace)
        let numberOfMatches = regex.numberOfMatches(in: str, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, (str as NSString).length))
        return numberOfMatches != 0
    }
    
}
